{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RWET Programming Exercise C\n",
    "\n",
    "To do this exercise, download this notebook and open it on your own computer. There are several tasks described below. Your job is to change the code in the cells so that the output from running the cell matches the expected output indicated above the cell."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 1: Making a dictionary\n",
    "\n",
    "Task 1: A variable `element_names` is defined below, with a value of `None`.\n",
    "Change the definition of `element_names` so that the variable contains a\n",
    "dictionary value, and the print statement below displays \"helium\" (instead\n",
    "of throwing a `KeyError`).\n",
    "\n",
    "Expected output: `helium`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "element_names = None\n",
    "print(element_names['He'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 2: Adding a new key to a dictionary\n",
    "\n",
    "Modify the values of the variables `key` and `val` below so that\n",
    "the print statement displays `lithium`.\n",
    "\n",
    "Expected output: `lithium`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = \"???\"\n",
    "val = \"???\"\n",
    "element_names[key] = val\n",
    "print(element_names['Li'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 3: Modifying an existing key\n",
    "\n",
    "Modifying the values of the variables `key` and `value` below\n",
    "so that the print statement displays `19`. (The goal here is to change the value for the key `wine`.)\n",
    "\n",
    "Expected output: `19`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = \"???\"\n",
    "val = \"???\"\n",
    "word_counts = {'cheese': 10, 'wine': 17, 'arachnids': 24}\n",
    "word_counts[key] = val\n",
    "print(word_counts['wine'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 4: Dictionary operations\n",
    "\n",
    "Change the value of the variable `planet_name` below so that the\n",
    "expected output is printed.\n",
    "\n",
    "Expected output: `Pluto is not a planet.`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "planet_classifications = {'Mercury': 'terrestrial', 'Venus': 'terrestrial',\n",
    "    'Earth': 'terrestrial', 'Mars': 'terrestrial', 'Jupiter': 'gas giant',\n",
    "    'Saturn': 'gas giant', 'Uranus': 'ice giant', 'Neptune': 'ice giant'}\n",
    "planet_name = 'Neptune'\n",
    "\n",
    "if planet_name in planet_classifications:\n",
    "    print(planet_name + \" is a planet.\")\n",
    "else:\n",
    "    print(planet_name + \" is not a planet.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 6: Keys and loops\n",
    "\n",
    "Modify the code below in the two following ways. First, change\n",
    "the right-hand side of the `planet_list` assignment. Then, add an `if`\n",
    "statement inside the `for` loop. Your goal is to produce the expected output.\n",
    "Use the `.keys()` method and the `sorted()` function. You're\n",
    "checking to see which keys have the value \"terrestrial\".\n",
    "\n",
    "Expected output:\n",
    "\n",
    "    Earth\n",
    "    Mars\n",
    "    Mercury\n",
    "    Venus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "planet_list = [] # <-- change this\n",
    "for planet in planet_list:\n",
    "    # add an if statement here, and tab over the line below\n",
    "    print(planet)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 7: Text analysis\n",
    "    \n",
    "Modify the code below in the two following ways. First, change\n",
    "the assignment of the variable `word_list` so that it contains a list\n",
    "of words in the string `words` (hint: use the `.split()` method). Second,\n",
    "replace the word `pass` in the `for` loop with a Python statement that\n",
    "adds a new key/value pair to the dictionary `word_dict`, indicating the\n",
    "length of word. The result should be that the final print statement\n",
    "displays the output \"4\".\n",
    "\n",
    "Expected output: 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "words = \"Mother said there'd be days like these.\"\n",
    "word_list = [] # <-- modify this\n",
    "word_dict = {}\n",
    "for word in word_list:\n",
    "    pass # <-- replace this\n",
    "print(word_dict['days'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 8: Text analysis, part 2\n",
    "\n",
    "Using the `word_dict` variable you created in the previous task,\n",
    "write a loop that displays the expected output. I've written a\n",
    "skeleton for you below. (You may need to use the `str()` function to\n",
    "append the two parts of the output string for each line together.)\n",
    "\n",
    "Expected output:\n",
    "\n",
    "    Mother: 6\n",
    "    said: 4\n",
    "    there'd: 7\n",
    "    be: 2\n",
    "    days: 4\n",
    "    like: 4\n",
    "    these.: 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for word in word_list:\n",
    "    pass # <-- replace this!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 9: Dictionaries with lists\n",
    "\n",
    "Modify the values of the variables \"key\" and \"val\" below so that\n",
    "the expected output is displayed.\n",
    "\n",
    "Expected output: `['foo', 'bar', 'baz']`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = \"???\"\n",
    "val = \"???\"\n",
    "widget_characteristics = {'scrumbulator': ['foo', 'bar']}\n",
    "widget_characteristics[key].append(val)\n",
    "print(widget_characteristics['scrumbulator'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 10: Lists of dictionaries\n",
    "\n",
    "Examine the data structure below: a list of dictionaries. Replace\n",
    "the word `pass` in the `for` loop below with a Python statement that takes\n",
    "the `demonym` key from each dictionary and appends it to the list `demonyms`,\n",
    "so that the expected output is produced.\n",
    "\n",
    "Expected output: Mercutian, Venutian, Earthling, Martian"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "planets = [\n",
    "    {'name': 'Mercury', 'type': 'terrestrial', 'demonym': 'Mercutian'},\n",
    "    {'name': 'Venus', 'type': 'terrestrial', 'demonym': 'Venutian'},\n",
    "    {'name': 'Earth', 'type': 'terrestrial', 'demonym': 'Earthling'},\n",
    "    {'name': 'Mars', 'type': 'terrestrial', 'demonym': 'Martian'}\n",
    "]\n",
    "\n",
    "demonyms = []\n",
    "for planet in planets:\n",
    "    pass # <-- replace this!\n",
    "print(\", \".join(demonyms))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 11: Sets\n",
    "\n",
    "The string below contains several duplications of the same strings.\n",
    "Insert an expression between the parentheses of the call to the `set()`\n",
    "function so that the desired output is produced. (Hint: You can initialize\n",
    "a new set by include an expression that evaluates to a string as the\n",
    "parameter to the `set()` function.)\n",
    "\n",
    "Expected output: `apple, banana, cupcake`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "items = \"apple apple banana apple cupcake banana apple cupcake\"\n",
    "items_set = set() # <-- insert an expression between the parentheses\n",
    "print(', '.join(sorted(items_set)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 12: List comprehensions (membership)\n",
    "\n",
    "The list comprehension below has no membership expression.\n",
    "Obviously, if left as-is, this list comprehension will evaluate to a copy\n",
    "of the original list. Add a membership expresison to the list comprehension\n",
    "so that the list comprehension evaluates to a new list that includes only\n",
    "those strings in the source list that start with the letter \"a\".\n",
    "\n",
    "Expected output: `aardvark, anteater, alpaca`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "animals = [\"aardvark\", \"camel\", \"anteater\", \"elephant\", \"alpaca\", \"jackal\"]\n",
    "animals_that_start_with_a = [x for x in animals] # <-- insert membership expr\n",
    "print(', '.join(animals_that_start_with_a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 13: List comprehensions (predicate expression)\n",
    "\n",
    "The list comprehension below has an expression `x` for its\n",
    "predicate expression. Modify the predicate expression so that the\n",
    "comprehension evaluates to a list containing the last four letters\n",
    "of each string in the source list.\n",
    "\n",
    "Expected output: `vark, amel, ater, hant, paca, ckal`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "animals = [\"aardvark\", \"camel\", \"anteater\", \"elephant\", \"alpaca\", \"jackal\"]\n",
    "animal_parts = [x for x in animals] # <-- replace leftmost \"x\"\n",
    "print(', '.join(animal_parts))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 14: List comprehensions (putting it all together)\n",
    "\n",
    "Modify both the predicate expression and the membership expression\n",
    "of the list comprehension below so that the comprehension evaluates to a\n",
    "list containing only those strings that do NOT begin with the letter `a`;\n",
    "the resulting strings should all have their first letters capitalized.\n",
    "(Hint: use the Python string object's `.title()` method.)\n",
    "\n",
    "Expected output: `Camel, Elephant, Jackal`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "animals = [\"aardvark\", \"camel\", \"anteater\", \"elephant\", \"alpaca\", \"jackal\"]\n",
    "animal_list = [x for x in animals] # <-- replace \"x\" and add membership expr\n",
    "print(', '.join(animal_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 15: List comprehensions with lists of dictionaries\n",
    "\n",
    "The `planets` variable below contains a list of dictionaries. Each\n",
    "dictionary in the list represents a planet, with a key/value pair for the\n",
    "planet's name and its type. Modify the predicate expression and membership\n",
    "expression of the list comprehension below so that the `dwarf_planet_names`\n",
    "variable contains a list of the names of each planet with a type of `dwarf`.\n",
    "(Hint: the `\"planet name\"` expression in the list comprehension is just\n",
    "a placeholder! Replace it with an expression that evaluates to the value\n",
    "for the `name` key in the dictionary.)\n",
    "\n",
    "Expected output: `Ceres, Pluto, Haumea, Makemake, Eris`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "planets = [\n",
    "    {'name': 'Mercury', 'type': 'terrestrial'},\n",
    "    {'name': 'Venus', 'type': 'terrestrial'},\n",
    "    {'name': 'Earth', 'type': 'terrestrial'},\n",
    "    {'name': 'Mars', 'type': 'terrestrial'},\n",
    "    {'name': 'Ceres', 'type': 'dwarf'},\n",
    "    {'name': 'Jupiter', 'type': 'jovian'},\n",
    "    {'name': 'Saturn', 'type': 'jovian'},\n",
    "    {'name': 'Uranus', 'type': 'ice giant'},\n",
    "    {'name': 'Neptune', 'type': 'ice giant'},\n",
    "    {'name': 'Pluto', 'type': 'dwarf'},\n",
    "    {'name': 'Haumea', 'type': 'dwarf'},\n",
    "    {'name': 'Makemake', 'type': 'dwarf'},\n",
    "    {'name': 'Eris', 'type': 'dwarf'},\n",
    "]\n",
    "dwarf_planet_names = [\"planet name\" for x in planets] # <-- make changes here!\n",
    "print(\", \".join(dwarf_planet_names))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
